from flask import Blueprint, render_template, request
from merchant_manager import MerchantManager
from werkzeug.utils import secure_filename
import os

merchant_bp = Blueprint('merchant', __name__)

# 实例化 MerchantManager
merchant_manager = MerchantManager()

@merchant_bp.route('/MerchantMenu', methods=['GET', 'POST'])
def merchant_menu():
    if request.method == 'GET':
        # 获取餐厅菜单列表
        username = "example_restaurant"  # 这里需要根据实际情况设置用户名
        menu = merchant_manager.get_menu_list(username)
        if menu:
            return render_template('MerchantMenu.html', username=username, result=menu, messages="done")
        else:
            return render_template('MerchantMenu.html', username=username, messages="none")
    elif request.form.get("action") == "删除该菜品":
        # 删除菜品
        dishname = request.form.get('dishname')
        restaurant = request.form.get('restaurant')
        success = merchant_manager.delete_dish(dishname, restaurant)
        return render_template('MerchantMenu.html', dmessages="done" if success else "fail")
    elif request.form.get("action") == "按销量排序":
        # 按销量排序
        username = "example_restaurant"  # 这里需要根据实际情况设置用户名
        menu = merchant_manager.sort_menu_by_sales(username)
        if menu:
            return render_template('MerchantMenu.html', username=username, result=menu, messages="done")
        else:
            return render_template('MerchantMenu.html', username=username, messages="none")
    elif request.form.get("action") == "按价格排序":
        # 按价格排序
        username = "example_restaurant"  # 这里需要根据实际情况设置用户名
        menu = merchant_manager.sort_menu_by_price(username)
        if menu:
            return render_template('MerchantMenu.html', username=username, result=menu, messages="done")
        else:
            return render_template('MerchantMenu.html', username=username, messages="none")

@merchant_bp.route('/MenuModify', methods=['GET', 'POST'])
def menu_modify():
    if request.form.get("action") == "修改菜品信息":
        dishname = request.form['dishname']
        restaurant = request.form['restaurant']
        dishinfo = request.form['dishinfo']
        nutriention = request.form.get('nutriention')
        price = request.form.get('price')
        isSpecialty = request.form.get('isSpecialty')
        return render_template('MenuModify.html', dishname=dishname, rest=restaurant, dishinfo=dishinfo,
                               nutriention=nutriention, price=price, isSpecialty=isSpecialty)
    elif request.form.get("action") == "提交修改":
        dishname = request.form.get('dishname')
        restaurant = request.form.get('rest')
        dishinfo = request.form.get('dishinfo')
        nutriention = request.form.get('nutriention')
        price = request.form.get('price')
        isSpecialty = int(request.form.get('isSpecialty'))
        f = request.files['imagesrc']
        imgsrc = None
        if f and merchant_manager.allowed_file(f.filename):
            filename = secure_filename(f.filename)
            f.save(os.path.join(merchant_manager.upload_folder[1:], filename))
            imgsrc = os.path.join(merchant_manager.upload_folder, filename)
        success = merchant_manager.update_dish(dishname, restaurant, dishinfo, nutriention, price, isSpecialty, imgsrc)
        return render_template('MenuModify.html', messages="done" if success else "fail")

@merchant_bp.route('/MenuAdd', methods=['GET', 'POST'])
def menu_add():
    if request.form.get("action") == "增加菜品":
        restaurant = request.form.get('restaurant')
        return render_template('MenuAdd.html', rest=restaurant)
    elif request.form.get("action") == "确认增加":
        dishname = request.form.get('dishname')
        restaurant = request.form.get('rest')
        dishinfo = request.form.get('dishinfo')
        nutriention = request.form.get('nutriention')
        price = request.form.get('price')
        isSpecialty = int(request.form.get('isSpecialty'))
        f = request.files['imagesrc']
        imgsrc = None
        if f and merchant_manager.allowed_file(f.filename):
            filename = secure_filename(f.filename)
            f.save(os.path.join(merchant_manager.upload_folder[1:], filename))
            imgsrc = os.path.join(merchant_manager.upload_folder, filename)
        success = merchant_manager.add_dish(dishname, restaurant, dishinfo, nutriention, price, imgsrc, isSpecialty)
        return render_template('MenuAdd.html', messages="done" if success else "fail")